package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/acl/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    //角色分页查询
    @ApiOperation("角色条件分页查询")
    @GetMapping("{current}/{limit}")
    public Result pageList(@PathVariable Long current,
                           @PathVariable Long limit,
                           RoleQueryVo roleQueryVo) {
        // 创建page，设置当前页，设置每页记录数
        // current 当前页, limit 每页记录数
        Page<Role> page = new Page<>(current, limit);
        // 调用service方法
        IPage<Role> pageModel = roleService.selectRolePage(page, roleQueryVo);
        return Result.ok(pageModel);
    }

    //根据id查询角色
    @ApiOperation("根据id查询角色")
    @GetMapping("get/{id}")
    public Result getRole(@PathVariable Long id) {
        Role role = roleService.getById(id);
        return Result.ok(role);
    }

    // 添加角色
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result saveRole(@RequestBody Role role) {
        boolean save = roleService.save(role);
        return save ? Result.ok(null) : Result.fail(null);
    }

    // 修改角色
    @ApiOperation("修改角色")
    @PutMapping("update")
    public Result updateRole(@RequestBody Role role) {
        boolean update = roleService.updateById(role);
        return update ? Result.ok(null) : Result.fail(null);
    }

    // 根据id删除角色
    @ApiOperation("根据id删除角色")
    @DeleteMapping("remove/{id}")
    public Result removeRole(@PathVariable Long id) {
        roleService.removeById(id);
        return Result.ok(id);
    }

    // 根据id批量删除角色
    @ApiOperation("根据id批量删除角色")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean remove = roleService.removeByIds(idList);
        return remove ? Result.ok(null) : Result.fail(null);
    }
}
